package com.gxa.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.uitls.GyUitls;
import com.gxa.common.uitls.JwtUtils;
import com.gxa.common.uitls.R;
import com.gxa.dto.UserDto;
import com.gxa.entity.User;
import com.gxa.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;


    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header", required = true),
            @ApiImplicitParam(name = "uname", value = "当前登录用户的账号，由后台从令牌中解析取得", required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回用户详细信息"),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })


    @PostMapping("/user")
    @ApiOperation("查询所有权限用户,并按名字查询")
    public R queryAll(@RequestBody UserDto userDto){
        try {
            Page<Map<String,Object>> pageHelperList = PageHelper.startPage(userDto.getPage(),userDto.getLimit());
            List<User> users =this.userService.queryAll(userDto);
            Map<String,Object> map =new HashMap<>();
            map.put("user",users);
            map.put("count",pageHelperList.getTotal());
            return R.ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询失败");
        }
    }



    @GetMapping("/user/queryTelByUname")
    @ApiOperation("点击用户信息，传输用户名，返回手机号")
    public R queryTelByUname(String uname){


        try {
            List<String> tel = this.userService.queryTelByUname(uname);

            Map<String,Object> map = new HashMap<>();
            map.put("user",tel);

            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("无手机号");
        }
    }

    @PostMapping("/user/updatePwdAndTel")
    @ApiOperation("修改密码手机号")
    public R updatePwdAndTel(@RequestBody UserDto userDto){

        String s = GyUitls.salt(userDto.getNewPassword()).toString();
        userDto.setNewPassword(s);
        try {
            this.userService.updatePwdAndTel(userDto);
            return R.ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("修改失败");
        }
    }

    @PostMapping("/user/updateLimt")
    @ApiOperation("修改用户权限等级")
    public  R updateLimit(@RequestBody UserDto userDto){
        try {
            this.userService.updateLimit(userDto);
            return R.ok("修改用户权限成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("修改用户权限失败");
        }
    }

    @GetMapping("/user/queryByOldPwdAndUname")
    @ApiOperation("验证用户与旧密码")
    public R login(String uname, String oldPassword){


        try {
           String saltPwd= GyUitls.salt(oldPassword).toString();

            User user = userService.queryByOldPwdAndUname(uname, saltPwd);
//            subject.login(oAuth2Token);

            Map<String,Object> map = new HashMap<>();
            map.put("user",user);

            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("密码错误");
        }
    }

}
